package cn.gailvlun.gll.widget

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.content.Context
import android.support.annotation.IntDef
import android.util.AttributeSet
import android.view.View
import android.widget.*
import cn.gailvlun.gll.R
import cn.gailvlun.gll.config.dip
import com.afollestad.materialdialogs.MaterialDialog
import com.jakewharton.rxbinding2.view.RxView
import java.util.concurrent.TimeUnit


const val START = 0L
const val RECORDING = 1L
const val PAUSING = 2L
const val RETRY_RECORDING = 3L
const val DONE = 4L
const val RUN_OUT_OF_TIME = 5L

@IntDef(START, RECORDING, PAUSING, RETRY_RECORDING, DONE, RUN_OUT_OF_TIME)
annotation class RecordState

interface OnStateChangeListener {
    fun onStateChange(@RecordState state: Long)
}

class RecordView : RelativeLayout {
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView()
    }

    @RecordState
    private var mState: Long = START

    var onStateChangeListener: OnStateChangeListener? = null

    fun setState(@RecordState state: Long) {
        mState = state
        onStateChangeListener?.onStateChange(state)
        when (state) {
            START -> {
                ivCenter?.visibility = View.VISIBLE
                tvCenter?.visibility = View.VISIBLE

                llLeft?.animate()?.alpha(0f)?.translationXBy(dip(26f).toFloat())?.setListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationEnd(animation: Animator?) {
                        super.onAnimationEnd(animation)
                        llLeft?.visibility = View.GONE
                    }
                })
                llRight?.animate()?.alpha(0f)?.translationXBy(dip(-26f).toFloat())?.setListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationEnd(animation: Animator?) {
                        super.onAnimationEnd(animation)
                        llRight?.visibility = View.GONE
                    }
                })
            }
            PAUSING -> {
                llLeft?.visibility = View.VISIBLE
                llRight?.visibility = View.VISIBLE
                llLeft?.animate()?.alpha(1f)?.translationXBy(dip(-26f).toFloat())?.setListener(null)
                llRight?.animate()?.alpha(1f)?.translationXBy(dip(26f).toFloat())?.setListener(null)
            }
            RETRY_RECORDING -> {
                llLeft?.animate()?.alpha(0f)?.translationXBy(dip(26f).toFloat())?.setListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationEnd(animation: Animator?) {
                        super.onAnimationEnd(animation)
                        llLeft?.visibility = View.GONE
                    }
                })
                llRight?.animate()?.alpha(0f)?.translationXBy(dip(-26f).toFloat())?.setListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationEnd(animation: Animator?) {
                        super.onAnimationEnd(animation)
                        llRight?.visibility = View.GONE
                    }
                })
            }
            RUN_OUT_OF_TIME -> {
                llLeft?.visibility = View.VISIBLE
                llRight?.visibility = View.VISIBLE
                llLeft?.animate()?.alpha(1f)?.translationXBy(dip(-26f).toFloat())?.setListener(null)
                llRight?.animate()?.alpha(1f)?.translationXBy(dip(26f).toFloat())?.setListener(null)

                ivCenter?.visibility = View.INVISIBLE
                tvCenter?.visibility = View.INVISIBLE
            }
        }
    }

    var isHasPermission = false

    private fun initView() {
        View.inflate(context, R.layout.hotel_record_view, this)
        llLeft = findViewById(R.id.ll_left)
        llRight = findViewById(R.id.ll_right)
        ivCenter = findViewById(R.id.iv_center)
        tvCenter = findViewById(R.id.tv_center)

        RxView.clicks(llLeft!!)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe {

                    MaterialDialog.Builder(context)
                            .title("删除已录制的内容，重新开始录制？")
                            .positiveText("删除")
                            .onPositive { dialog, which ->
                                ivCenter?.setImageResource(R.drawable.ic_record)
                                tvCenter?.text = "开始录制"
                                setState(START)

                            }
                            .negativeText("取消")
                            .show()
                }

        RxView.clicks(ivCenter!!)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe {
                    when (mState) {
                        START -> {
                            if (isHasPermission) {
                                ivCenter?.setImageResource(R.drawable.ic_pause)
                                tvCenter?.text = "暂停"
                                setState(RECORDING)
                            } else {
                                Toast.makeText(context, "Don't have permission yet", Toast.LENGTH_SHORT).show()
                            }
                        }
                        RECORDING -> {
                            ivCenter?.setImageResource(R.drawable.ic_record)
                            tvCenter?.text = "继续录制"
                            setState(PAUSING)
                        }
                        PAUSING, DONE -> {
                            ivCenter?.setImageResource(R.drawable.ic_pause)
                            tvCenter?.text = "暂停"
                            setState(RETRY_RECORDING)
                        }
                        RETRY_RECORDING -> {
                            ivCenter?.setImageResource(R.drawable.ic_record)
                            tvCenter?.text = "继续录制"
                            setState(PAUSING)
                        }
                    }
                }
        llRight?.setOnClickListener {
            setState(DONE)
        }
    }

    private var llLeft: LinearLayout? = null
    private var llRight: LinearLayout? = null
    private var ivCenter: ImageView? = null
    private var tvCenter: TextView? = null


    fun clear(){
        ivCenter?.setImageResource(R.drawable.ic_record)
        tvCenter?.text = "开始录制"
    }
}